package tank.task.lagou.framework;

import org.apache.catalina.*;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import tank.task.lagou.demo.DemoController;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.File;


public class SpringApplication {

    public  static  void run() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        try {
            new SpringApplication().run(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void run(Boolean useSpiLoad) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ServletException {
        Tomcat tomcatServer = new Tomcat();//创建tomcat
        //初始化tomcat
        if(useSpiLoad){//使用spi模式加载springmvc
            initSpiTomcat(tomcatServer);
        }else{ //不适用spi模式加载 springmvc
            initTomcat(tomcatServer);
        }
        try {
            //启动tomcat
            tomcatServer.start();
            //等待tomcat完成
            tomcatServer.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    private void initSpiTomcat(Tomcat tomcatServer) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ServletException {
        //此tomcat端口是否自动部署
        tomcatServer.getHost().setAutoDeploy(false);
        //创建一个web应用程序
        StandardContext standardContex = new StandardContext();
        //设置web应用程序的上下文地址
        standardContex.setPath("/");
        //添加web应用程序的监听
        //standardContex.addLifecycleListener(new Tomcat.FixContextListener());

        //将web应用程序添加到服务器中
        tomcatServer.getHost().addChild(standardContex);

        // 主动加载web的生命周期，这样就可以使用WebApplicationInitializer接口了
        standardContex.addLifecycleListener((LifecycleListener) Class.forName(tomcatServer.getHost().getConfigClass()).newInstance());



    }

    private void initTomcat(Tomcat tomcatServer) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //此tomcat端口是否自动部署
        tomcatServer.getHost().setAutoDeploy(false);
        //创建一个Contex
        //创建一个web应用程序
        StandardContext standardContex = new StandardContext();
        //设置web应用程序的上下文地址
        standardContex.setPath("/app");
        //添加web应用程序的监听

        standardContex.addLifecycleListener(new Tomcat.FixContextListener());
        //将web应用程序添加到服务器中


        tomcatServer.getHost().addChild(standardContex);

        //将SpringMVC的dispatcherServlet加入到tomcat中
        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext;
        annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
        //注册spriingmvc的ComponentScan注解扫描Bean

        annotationConfigWebApplicationContext.register(AppConfig.class);
        annotationConfigWebApplicationContext.refresh();
        System.out.println(annotationConfigWebApplicationContext.getBean(DemoController.class));
        tomcatServer.addServlet("/app", "dispatcherServlet", new DispatcherServlet(annotationConfigWebApplicationContext));
        standardContex.addServletMappingDecoded("/*", "dispatcherServlet");

    }


}
